package com.example.demo.controller;

import com.example.demo.entity.Employee;
import com.example.demo.mapper.EmployeeMapper;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;
import java.util.List;

@Controller

public class EmployeeController {

    @Autowired
    private EmployeeMapper employeeMapper;

    @RequestMapping("/emps")
    public String list(Model model){
        List<Employee> employees = employeeMapper.selectAll();
        model.addAttribute("emps",employees);
        System.out.println("进入查询页面");
        return "emp/list.html";
    }

//    添加员工
    @GetMapping("/emp")
    public String toaddpage(Model model){
        System.out.println("进入添加员工页面");
        return "emp/add";
    }
    @PostMapping("/emp")
    public String addEmp (Employee employee){

        System.out.println("添加员工成功=>"+employee);
        employeeMapper.insert(employee);
        return "redirect:/emps";
    }

    //去员工的修改页面
    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id")Integer id,Model model){
        //查出原来的数据
        Employee employee = employeeMapper.getEmployee(id);
        model.addAttribute("emp",employee);
        System.out.println("去员工的修改页面");
        return "emp/update";
    }

//    更新员工
    @PostMapping("/updateEmp")
    public String updateEmp(Employee employee){
        employeeMapper.updateByPrimaryKey(employee);

        return "redirect:/emps";
    }

    //删除员工
    @GetMapping("/delemp/{id}")
        public String deleteEmp(@PathVariable("id")int id){
//        employeeDao.delete(id);
        employeeMapper.deleteByPrimaryKey(id);
        return "redirect:/emps";
    }
}
